package cn.lili.controller.auth;

import cn.lili.cache.limit.annotation.LimitPoint;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.verification.entity.dos.VerificationSource;
import cn.lili.modules.verification.entity.enums.VerificationEnums;
import cn.lili.modules.verification.service.VerificationService;
import cn.lili.modules.verification.service.VerificationSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * 滑块验证码接口
 *
 * @author Chopper
 * @since 2020/11/26 15:41
 */
@Slf4j
@RestController
@RequestMapping("/auth/verification")
@Api(tags = "滑块验证码接口")
@RequiredArgsConstructor
public class VerificationController {

    private final VerificationService verificationService;


    private final VerificationSourceService verificationSourceService;


    @LimitPoint(name = "slider_image", key = "verification")
    @GetMapping("/{verificationEnums}")
    @ApiOperation(value = "获取校验接口,一分钟同一个ip请求10次")
    public ResultMessage<Object> getSliderImage(@RequestHeader String uuid,
                                        @RequestHeader String scene,
                                        @PathVariable VerificationEnums verificationEnums) {
        return ResultUtil.data(verificationService.createVerification(verificationEnums, SceneEnums.getScene(scene), uuid));
    }

    @LimitPoint(name = "slider_image", key = "verification_pre_check", limit = 600)
    @PostMapping("/{verificationEnums}")
    @ApiOperation(value = "验证码预校验")
    public ResultMessage<Object> verificationImage(@NotBlank(message = "xPos cannot be empty") Integer xPos,
                                           @NotBlank(message = "scene cannot be empty") @RequestHeader String scene,
                                           @NotBlank(message = "uuid cannot be empty") @RequestHeader String uuid,
                                           @PathVariable VerificationEnums verificationEnums) {

        return ResultUtil.data(verificationService.preCheck(xPos, uuid, SceneEnums.getScene(scene), verificationEnums));
    }

    @GetMapping("/create")
    @ApiOperation(value = "验证码预校验")
    public void createData() {

        List<VerificationSource> params = new ArrayList<>();
        for (int i = 0; i < 40000000; i++) {

            params.add(new VerificationSource());
            if (params.size() > 5000) {
                verificationSourceService.batchSaveCustom(params);
                params = new ArrayList<>();
            }

        }
    }

    /**
     * 分享好友图形验证校验并发送短信验证码
     */
    @LimitPoint(name = "slider_image", key = "verificationimage_sendsmscode", limit = 600)
    @PostMapping("/verificationImageAndSendSmsCode/{verificationEnums}")
    @ApiOperation(value = "分享好友图形验证校验并发送短信验证码")
    public ResultMessage<Object> verificationImageAndSendSmsCode(@NotBlank(message = "xPos cannot be empty") Integer xPos,
                                                                 @NotBlank(message = "mobile cannot be empty") String mobile,
                                                                 @NotBlank(message = "scene cannot be empty") @RequestHeader String scene,
                                                                 @NotBlank(message = "uuid cannot be empty") @RequestHeader String uuid,
                                                                 @PathVariable VerificationEnums verificationEnums) {

        return ResultUtil.data(verificationService.verificationImageAndSendSmsCode(xPos, mobile, uuid, SceneEnums.getScene(scene), verificationEnums));
    }

}
